
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Airlangga
 */
public class GridView {

    private char[][] cell;
    private ProjModel pm;
    private int gridSize;
    private ArrayList<Car> carArray;
    private int ii = 0;

    public GridView() {
    }

    /** Grid constructor */
    public GridView(ProjModel pm) {
        this.pm = pm;
        cell = new char[pm.getGridSize()][pm.getGridSize()];
        gridSize = pm.getGridSize();
        carArray = new ArrayList<Car>();
    }

    /** initialize cell with char 'x' */
    public void initGrid(char ch, int cycle) {
        //for(; ii < cycle; ii++){
        for (int i = 0; i < gridSize; i++) {
            for (int j = 0; j < gridSize; j++) {
                cell[i][j] = ch;
            }
        }

        buildHorLane();
        buildVerLane();
        buildHorCars(pm.getHorProb(), pm.getHorLanes());
        buildVerCars(pm.getVerProb(), pm.getVerLanes());
        for (int i = 0; i < carArray.size(); i++) {
            if (carArray.get(i).getDirection() == 'h') {
                moveHor(i);
            }
            if (carArray.get(i).getDirection() == 'v') {
                moveVer(i);
            }

        }

        showCars();
        showGrid();
    }

    /** Shows Grid */
    public void showGrid() {
        for (int i = 0; i < gridSize; i++) {
            for (int j = 0; j < gridSize; j++) {
                System.out.print(cell[i][j]);
            }
            System.out.println();
        }
    }

    /** Method for creating horizontal lane */
    public void buildHorLane() {
        int middle = gridSize / 2;
        //if user set horLane = 3
        int horLane = pm.getHorLanes();

        for (int i = 0; i < gridSize; i++) {
            for (int c = 0; c < horLane; c++) {
                cell[middle + c][i] = ' ';
            }
        }
    }

    /** Method for creating vertical lane */
    public void buildVerLane() {
        int middle = gridSize / 2;
        int verLane = pm.getVerLanes();
        for (int i = 0; i < gridSize; i++) {
            for (int c = 0; c < verLane; c++) {
                cell[i][middle + c] = ' ';
            }
        }
    }

    /** Method for creating horizontal cars */
    public void buildHorCars(double horProb, int horLanes) {
        int middle = gridSize / 2;

        Random rnd = new Random();
        if (pm.getHorProb() > rnd.nextFloat()) {
            if (horLanes == 1) {
                carArray.add(new Car(middle, 0, 'h'));
            } else if (horLanes == 2) {
                carArray.add(new Car((middle + (int) (Math.random() * 2)), 0, 'h'));
            } else if (horLanes == 3) {
                carArray.add(new Car((middle + (int) (Math.random() * 3)), 0, 'h'));
            }
        }


    }

    /** Method for creating vertical cars */
    public void buildVerCars(double verProb, int verLanes) {
        int middle = gridSize / 2;  //gridSize =20; middle = 10

        Random rnd = new Random();
        if (pm.getVerProb() > rnd.nextFloat()) {
            if (verLanes == 1) {
                carArray.add(new Car(0, middle, 'v'));
            } else if (verLanes == 2) {
                carArray.add(new Car(0, (middle + (int) (Math.random() * 2)), 'v'));
            } else if (verLanes == 3) {
                carArray.add(new Car(0, (middle + (int) (Math.random() * 3)), 'v'));
            } else if (verLanes == 4) {
                carArray.add(new Car(0, (middle + (int) (Math.random() * 4)), 'v'));
            }
        }


    }

    /** show cars */
    public void showCars() {
        for (int i = 0; i < carArray.size(); i++) {
            cell[carArray.get(i).getXPos()][carArray.get(i).getYPos()] = 'c';
        }
    }

    public void moveHor(int cycle) {
        carArray.get(cycle).setYPos((carArray.get(cycle).getYPos()) + 1);

        int x = carArray.get(cycle).getXPos();
        int y = carArray.get(cycle).getYPos();
        if (y >= 30) {
            carArray.remove(cycle);
        } else {
            cell[x][y] = 'c';
            cell[x][y - 1] = ' ';
        }

    }

    public void moveVer(int cycle) {
        carArray.get(cycle).setXPos((carArray.get(cycle).getXPos()) + 1);

        int x = carArray.get(cycle).getXPos();
        int y = carArray.get(cycle).getYPos();
        if (x >= 30) {
            carArray.remove(cycle);
        } else {
            cell[x][y] = 'c';
            cell[x - 1][y] = ' ';
        }

    }
}
